package com.study.controller;

import com.study.bean.SuccessLogin;
import com.study.bean.User;
import com.study.config.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author MI
 * @ClassName HelloController.java
 * @createTime 2021年08月10日 10:54:00
 */
@Api(tags = "Hello-测试")
@Slf4j
@Controller
public class HelloController {

    @Autowired
    IdWorker idWorker;

    @GetMapping("pic/1")
    public void pic(HttpServletResponse response) throws IOException {
        File file = ResourceUtils.getFile("classpath:static/12.jpg");
        IOUtils.copy(FileUtils.openInputStream(file), response.getOutputStream());
    }

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("msg", "你好 thymeleaf 2");
        model.addAttribute("links", "http://www.baidu.com");
        return "success";
    }

    @ApiOperation(value = "用户-登录)", notes = "详细描述-获取大佬的粉丝")
    @ResponseBody
    @PostMapping("login")
    public SuccessLogin login(@ApiParam(value = "登录的用户", required = true) User user) {
        return new SuccessLogin("a", LocalDate.now());
    }

    @ApiOperation(value = "雪花ID", notes = "详细描述-雪花ID")
    @GetMapping("id")
    @ResponseBody
    public long idWork() {
        log.info("mybatis getIdStr:{}", com.baomidou.mybatisplus.core.toolkit.IdWorker.getIdStr());
        log.info("mybatis getId:{}", com.baomidou.mybatisplus.core.toolkit.IdWorker.getId());

        log.info("ID:{}", idWorker.nextId());
        return idWorker.nextId();
    }
}
